/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.datasource.routing;

/**
 * DataSourceContext
 *
 * @auther ebert_chan
 */
public final class DataSourceContext {

    private static final ThreadLocal<DataSourceContext> threadLocal = new ThreadLocal<DataSourceContext>();

    private String target;

    private boolean slave;

    /**
     * @param target
     */
    public DataSourceContext(String target) {
        this(target, Boolean.FALSE);
    }

    /**
     * @param target
     * @param slave
     */
    public DataSourceContext(String target, boolean slave) {
        super();
        this.target = target;
        this.slave = slave;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public boolean isSlave() {
        return slave;
    }

    public void setSlave(boolean slave) {
        this.slave = slave;
    }

    // public static methods

    public static void set(String target, boolean slave) {
        DataSourceContext context = new DataSourceContext(target, slave);
        threadLocal.set(context);
    }

    public static void set(DataSourceContext context) {
        threadLocal.set(context);
    }

    public static void useMaster(String target) {
        set(target, Boolean.FALSE);
    }

    public static void useSlave() {
        set("", Boolean.TRUE);
    }

    public static DataSourceContext get() {
        return threadLocal.get();
    }

    public static void clear() {
        threadLocal.remove();
    }

}
